<?php
// @formatter:off
/**
 * @file WCTableRepresentation.abs.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-08-05 00:26:28 +0000 (Mon, 05 Aug 2013) $
 *
 * $Id: WCTableRepresentation.abs.php 100 2013-08-05 00:26:28Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.2/includes/representations/WCTableRepresentation.abs.php $
 */
// @formatter:on

/**
 * @abstract
 * @class WCTableRepresentation This class is an extension of the abstract
 * representation of database table rows made by class 'WCRepresentation'. This
 * is necessary because of the lack of some generic method on parent class.
 */
abstract class WCTableRepresentation extends WCRepresentation {
	//
	// Public methods.
	/**
	 * This method loads an item's information from the database. This
	 * initializes the representation.
	 *
	 * @param int $id Item's identifier.
	 * @return boolean Returns true if the item was found an successfully
	 * loaded.
	 */
	public function load($id) {
		//
		// Setting a default item status.
		$this->_ok = false;
		//
		// Filling basic item's information.
		$this->_info = array("{$this->_PropertiesPrefix}id" => $id);
		//
		// Creating a statement to retrieve item's data from database.
		// @{
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__."[".$this->_PropertiesTable."]";
		if($this->_db->has($stmtId)) {
			$stmt = $this->_db->get($stmtId);
		} else {
			$prefix = $this->_PropertiesPrefix;
			$table = $this->_dbprefix.$this->_PropertiesTable;

			$query = "select  *\n";
			$query .= "from    {$table}\n";
			$query .= "where   {$prefix}id = :id\n";
			$stmt = $this->_db->prepare($stmtId, $query);
		}
		// @}
		//
		// Building a database parameters.
		$params = array(":id" => $this->id);
		//
		// Retrieving information.
		$result = $stmt->execute($params);
		//
		// Checking information.
		if($result && $stmt->rowCount() == 1) {
			//
			// The retrieve row is directly used as information.
			$this->_info = $stmt->fetch();
			//
			// At this point, this item is considered to be
			// successfully loaded.
			$this->_ok = true;
			//
			// Converting each boolean property into a real boolean
			// value.
			foreach($this->_PropertiesBoolean as $property) {
				$prop = $this->_PropertiesPrefix.$property;
				$this->_info[$prop] = isset($this->_info[$prop]) ? $this->_info[$prop] == "Y" : false;
			}
		} elseif(!$result) {
			//
			// At this point, database didn't return results.
			//
			// Loading global pointers.
			global $wcLog;
			//
			// Building a error message.
			$error = $stmt->errorInfo();
			$msg = __CLASS__."::".__FUNCTION__."(): There was an error trying to load id '{$id}' from table '{$this->_dbprefix}{$this->_PropertiesTable}'.";
			$msg .= " Error: [{$error[1]}] {$error[2]}";
			//
			// Informing error on logs.
			if($wcLog) {
				$wcLog->write($msg);
				///@todo check level;
			}
			//
			// Since this is a fatal problem, an exception is thrown.
			trigger_error($msg, E_USER_WARNING);
		} elseif($stmt->rowCount() != 1) {
			//
			// At this point, database have run the statement without
			// problems, but nothing was retrieved matching filters.
			//
			// Loading global pointers.
			global $wcLog;
			//
			// Building a error message.
			$msg = __CLASS__."::".__FUNCTION__."(): There was an error trying to load id '{$id}' from table '{$this->_dbprefix}{$this->_PropertiesTable}'.";
			$msg .= " Resulting rows count: ".$stmt->rowCount();
			//
			// Informing error on logs.
			if($wcLog) {
				$wcLog->write($msg);
				///@todo check level;
			}
			//
			// Since this is a fatal problem, an exception is thrown.
			trigger_error($msg, E_USER_WARNING);
		}
		//
		// Returning final item's status.
		return $this->ok();
	}
	/**
	 * This method loads an item's information from the database based on its
	 * name. This initializes the representation.
	 *
	 * @param string $name Item's name.
	 * @return boolean Returns true if the item was found an successfully
	 * loaded.
	 */
	public function loadByName($name) {
		//
		// Setting a default item status.
		$this->_ok = false;
		//
		// Filling basic item's information.
		$this->_info = array();
		//
		// Creating a statement to retrieve item's data from database.
		// @{
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__."[".$this->_PropertiesTable."]";
		if($this->_db->has($stmtId)) {
			$stmt = $this->_db->get($stmtId);
		} else {
			$prefix = $this->_PropertiesPrefix;
			$table = $this->_dbprefix.$this->_PropertiesTable;
			$cname = $this->_PropertiesNameColumn;

			$query = "select  {$prefix}id as id,\n";
			$query .= "        {$prefix}{$cname} as name\n";
			$query .= "from    {$table}\n";
			$query .= "where   {$prefix}{$cname} = :name\n";
			$stmt = $this->_db->prepare($stmtId, $query);
		}
		// @}
		//
		// Building a database parameters.
		$params = array(":name" => $name);
		//
		// Retrieving information.
		$result = $stmt->execute($params);
		//
		// Checking information.
		if($result && $stmt->rowCount() == 1) {
			//
			// Fetching information.
			$row = $stmt->fetch();
			//
			// Name's checked only to ensure case sensitive match.
			if($name == $row["name"]) {
				//
				// Calling the real item's loader.
				$this->load($row["id"]);
			}
		} elseif(!$result) {
			//
			// At this point, database didn't return results.
			//
			// Loading global pointers.
			global $wcLog;
			//
			// Building a error message.
			$error = $stmt->errorInfo();
			$msg = __CLASS__."::".__FUNCTION__."(): There was an error trying to load {$this->_PropertiesNameColumn} '{$name}' from table '{$this->_dbprefix}{$this->_PropertiesTable}'.";
			$msg .= " Error: [{$error[1]}] {$error[2]}";
			//
			// Informing error on logs.
			if($wcLog) {
				$wcLog->write($msg);
				///@todo check level;
			}
			//
			// Since this is a fatal problem, an exception is thrown.
			trigger_error($msg, E_USER_WARNING);
		} elseif($stmt->rowCount() != 1) {
			//
			// At this point, database have run the statement without
			// problems, but nothing was retrieved matching filters.
			//
			// Loading global pointers.
			global $wcLog;
			//
			// Building a error message.
			$msg = __CLASS__."::".__FUNCTION__."(): There was an error trying to load {$this->_PropertiesNameColumn} '{$name}' from table '{$this->_dbprefix}{$this->_PropertiesTable}'.";
			$msg .= " Resulting rows count: ".$stmt->rowCount();
			//
			// Informing error on logs.
			if($wcLog) {
				$wcLog->write($msg);
				///@todo check level;
			}
			//
			// Since this is a fatal problem, an exception is thrown.
			trigger_error($msg, E_USER_WARNING);
		}
		//
		// Returning final item's status.
		return $this->ok();
	}
	/**
	 * This method triggers the represented item to be loaded again.
	 *
	 * @return boolean Returns true when the object was successfully reloaded.
	 */
	public function reload() {
		return $this->load($this->id);
	}
}
?>